package com.talkweb.securitycamera;

import com.talkweb.securitycamera.common.base.BaseAbility;
import com.talkweb.securitycamera.common.constans.Constant;
import com.talkweb.securitycamera.slice.EmptySlice;
import com.talkweb.securitycamera.utils.eventlib.communicationevent.MsgConstant;
import com.talkweb.securitycamera.utils.eventlib.communicationevent.PhotoEvent;
import com.talkweb.securitycamera.utils.eventlib.communicationevent.PhotoEventManager;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;

public class MainAbility extends BaseAbility {

    @Override
    public Class<com.talkweb.securitycamera.slice.EmptySlice> getSlice() {
        return EmptySlice.class;
    }

    @Override
    public String[] getNeedPermissions() {
        String[] permissions = null;
        try {
            permissions = getResourceManager().getElement(ResourceTable.Strarray_permissions).getStringArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return permissions;
    }

    @Override
    public void onReqPerMissionSuccess() {
        super.onReqPerMissionSuccess();
        getUITaskDispatcher().asyncDispatch(() -> {
           // findComponentById(ResourceTable.Id_layout_main).setVisibility(Component.VISIBLE);
           // findComponentById(ResourceTable.Id_layout_load).setVisibility(Component.HIDE);
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.talkweb.securitycamera")
                    .withAbilityName("com.talkweb.securitycamera.CameraViewAbility")
                    .build();

            Intent intent = new Intent();
            intent.setOperation(operation);

            setAbilitySliceAnimator(null);
            startAbility(intent);
            terminateAbility();
        });

    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        if (resultCode == Constant.DATA_CHANGED_CODE) {
            if (resultData != null) {
                boolean hasChange = resultData.getBooleanParam(Constant.HAS_CHANGED, false);
                if (hasChange) {
                    PhotoEvent event = PhotoEvent.obtain().setTopic(MsgConstant.EventUserTopic.COMMON_EVENT_PIC_DATA_CHANGR).setMsgObj(true);
                    PhotoEventManager.getInstance().post(event);
                }
            }
        }
    }
}
